package com.coffee.controller;


import com.coffee.common.BaseContext;
import com.coffee.common.GlobalConstant;
import com.coffee.entity.*;
import com.coffee.service.OrderService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     * @param orders
     * @return
     */
    @PostMapping("/make")
    public R<String> make(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success(GlobalConstant.FINISH);
    }

    /**
     * 取消订单
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Orders orders){
        String orderId = orders.getId();
        String userId = BaseContext.get();
       orderService.remove(orderId,userId);
        return R.success(GlobalConstant.FINISH);
    }

    /**
     * 查询订单明细
     * @return
     */
    @GetMapping("/listDetail")
    public R<List<OrderDetail>> listDet(String orderId){
        List<OrderDetail> list = orderService.listDet(orderId);
        return R.success(list);
    }



    /**
     * 查看用户订单
     * @return
     */
    @GetMapping("/listOrder")
    public R<List<Orders>> listOrd(){
        String userId = BaseContext.get();
        List<Orders> list = orderService.listOrd(userId);
        return R.success(list);
    }

    /**
     * pc端订单分页查询
     * @return
     */
    @GetMapping("/page")
    public R<PageResult> queryPage(QueryPageBean queryPageBean){
        PageResult pageResult = orderService.pageQuery(queryPageBean);
        return  R.success(pageResult);

    }

    /**
     * pc端批量删除订单记录
     * @param ids
     * @return
     */
    @DeleteMapping("/removeBatch")
    public R<String> deleteOrders(@RequestBody List<String> ids){
        orderService.removeBatch(ids);
        return R.success(GlobalConstant.FINISH);
    }

    /**
     * 统计分析
     *
     * @return
     */
    @GetMapping("/statistics")
    public R<HashMap<String, Integer>> listStatistics(){
     HashMap<String,Integer>staMap=orderService.getStatistics();
        return R.success(staMap);
    }

    /**
     * 季度咖啡销售图
     * @return
     */
    @GetMapping("/analyse")
    public R<HashMap<String, Integer>> listAnalyse(){
        HashMap<String,Integer>analyseMap=orderService.getAnalyse();
        return R.success(analyseMap);
    }

    @PostMapping("/status")
    public R status(@RequestBody Orders orders) {
        orderService.status(orders);
        return R.success(GlobalConstant.FINISH);
    }

}
